package com.etc.dao.impl;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.List;
import com.etc.dao.DiscussDao;
import com.etc.entity.Customer;
import com.etc.entity.Discuss;
import com.etc.entity.Games;
import com.etc.util.DBUtil;

public class DiscussDaoImpl implements DiscussDao{
//	查询所有一级评论
	@Override
	public List<Discuss> findAllByGamesId(Integer gamesId) {
		List<Discuss> list = new ArrayList<Discuss>();

		ResultSet rs1 = DBUtil.query("select a.discuss_id, a.games_id, a.customer_id, b.nickname, a.content, a.discuss_time, a.comment, a.reply_id from discuss a left join customer b on a.customer_id = b.customer_id where games_id = ? and reply_id is null", gamesId);
		try {
			while (rs1.next()) {
				// 评论
				Integer discussId = rs1.getInt("discuss_id");
				gamesId = rs1.getInt("games_id");
				Integer customerId = rs1.getInt("customer_id");
				String nickname = rs1.getString("nickname");
				String content = rs1.getString("content");
				String discussTime = rs1.getString("discuss_time");
				// 对日期格式进行转换
				int lastIndexOf = discussTime.lastIndexOf(".");
				String time = discussTime.substring(0, lastIndexOf);
				

				Integer comment = rs1.getInt("comment");
				
				// 将每一条记录封装成一个Java对象
				Discuss discuss = new Discuss();
				discuss.setDiscussId(discussId);
				discuss.setGamesId(gamesId);
				discuss.setCustomerId(customerId);
				discuss.setContent(content);
				discuss.setDiscussTime(time);

				discuss.setComment(comment);
				Customer customer = new Customer();
				customer.setNickname(nickname);
				discuss.setCustomer(customer);
				// 存储到集合中
				list.add(discuss);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	/**
	 * 通过评论编号查询该游戏的所有下一级评论
	 */
	@Override
	public List<Discuss> findAllByDiscussId(Integer discussId) {
		List<Discuss> list = new ArrayList<Discuss>();

		ResultSet rs1 = DBUtil.query("select a.discuss_id, a.games_id, a.customer_id, c.nickname , a.content, a.discuss_time, a.comment, a.reply_id, d.nickname from discuss a left join customer c on a.customer_id = c.customer_id left join discuss b on a.reply_id = b.discuss_id left join customer d on b.customer_id = d.customer_id where a.reply_id = ?", discussId);
		try {
			while (rs1.next()) {
				// 评论
				discussId = rs1.getInt("discuss_id");
				Integer gamesId = rs1.getInt("games_id");
				Integer customerId = rs1.getInt("customer_id");

				String replyName = rs1.getString(4);
				String content = rs1.getString("content");
				String discussTime = rs1.getString("discuss_time");
				// 对日期格式进行转换
				DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
				int lastIndexOf = discussTime.lastIndexOf(".");
				String time = discussTime.substring(0, lastIndexOf);

				Integer comment = rs1.getInt("comment");
				String byReplyName = rs1.getString(9);
				
				// 将每一条记录封装成一个Java对象
				Discuss discuss = new Discuss();
				discuss.setDiscussId(discussId);
				discuss.setGamesId(gamesId);
				discuss.setCustomerId(customerId);
				discuss.setContent(content);
				discuss.setDiscussTime(time);
				discuss.setComment(comment);
				discuss.setReplyName(replyName);
				discuss.setByReplyName(byReplyName);
				
				// 存储到集合中
				list.add(discuss);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	/**
	 * 通过评论编号删除
	 */
	@Override
	public int delete(Integer discussId) {
		
		return DBUtil.update("delete from discuss where discuss_id = ?", discussId);
	}
	/**
	 * 添加评论或回复
	 */
	@Override
	public int add(Discuss discuss) {
		
		return DBUtil.update("insert into discuss(games_id,customer_id,content,reply_id,comment) values(?,?,?,?,?)", discuss.getGamesId(), discuss.getCustomerId(), discuss.getContent(), discuss.getReplyId(), discuss.getComment());
	}

}
